package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;	
	
	private SortedListOf<ContactData> cacheContacts;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public SortedListOf<ContactData> getContacts() {
		if (cacheContacts == null){
			rebuildCacheContacts();
		}
		return cacheContacts;
	}

	private void rebuildCacheContacts() {
        cacheContacts = new SortedListOf<ContactData>();
        List<WebElement> lines = driver.findElements(By.name("entry"));
        for (WebElement line : lines)
        {
                List<WebElement> cells = line.findElements(By.tagName("td"));

                ContactData contact = new ContactData()
                                .withLastname(cells.get(1).getText())
                                .withFirstname(cells.get(2).getText());
             cacheContacts.add(contact);   
       }
}
	
	public ContactHelper creationContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();	
		returnToHomePage();
		//rebuildCacheContacts();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {		
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		//rebuildCacheContacts();
		return this;
	}

	public ContactHelper deleteContact(int index) {		
		manager.navigateTo().mainPage();
		initContactModification(index);
		submitContactDeletion();		
		returnToHomePage();	
		//rebuildCacheContacts();
		return this;
	}
	
	//---------------------------------------------------------------------------

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHometel());
		type(By.name("mobile"), contact.getMobiltel());
		type(By.name("work"), contact.getWorktel());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		type(By.name("address2"), contact.getAddressdop());
		type(By.name("phone2"), contact.getPhone2());
		
		if (formType == CREATION){
			
		} else{
			if(driver.findElements(By.name("new_group")).size()!=0){
				throw new Error("Group selector exists in contact modification form");
			}
		}	
		return this;			
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cacheContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper submitContactModification() {
        click(By.xpath("(//input[@name='update'])"));
		cacheContacts = null;
		return this;
	}


	public void initContactModification(int index) {
		index++;
		click(By.xpath("//table[@id='maintable']/tbody/tr["+index+"]/td[7]/a/img"));
	}
	
	public ContactHelper submitContactDeletion() {
		click(By.xpath("//*[@id='content']/form[2]/input[2]"));
		cacheContacts = null;
		return this;
	}
	

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}	    
}
